/*
 * Copyright 2005-2013 sxhuayuan.com. All rights reserved.
 */
package com.sxhuayuan.parking.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.sxhuayuan.parking.compenent.CommonAttributes;
import com.sxhuayuan.parking.compenent.LogConfig;
import com.sxhuayuan.parking.service.LogConfigService;
import com.sxhuayuan.parking.utils.SettingUtils;

/**
 * Service - 日志配置
 * 
 * @author gaoyan
 * @version 1.0
 */
@Service("logConfigServiceImpl")
public class LogConfigServiceImpl implements LogConfigService {

	@Cacheable("logConfig")
	public List<LogConfig> getAll() {
		try {
			InputStream resourceAsStream = SettingUtils.class.getClassLoader().getResourceAsStream(CommonAttributes.SETTINGS_XML_PATH);
			Document document = new SAXReader().read(resourceAsStream);
			List<Node> nodes = document.selectNodes("/sxhuayuan/logConfig");
			List<LogConfig> logConfigs = new ArrayList<LogConfig>();
			for (Node node : nodes) {
				Element e = (Element) node;
				String operation = e.attributeValue("operation");
				String urlPattern = e.attributeValue("urlPattern");
				LogConfig logConfig = new LogConfig();
				logConfig.setOperation(operation);
				logConfig.setUrlPattern(urlPattern);
				logConfigs.add(logConfig);
			}
			return logConfigs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}